package tk.leafotto.sign2.dao;

import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import java.util.ArrayList;

import tk.leafotto.sign2.bean.Player;
import tk.leafotto.sign2.bean.Sign;
import tk.leafotto.sign2.utils.PinYin;

public class DbUtils {

    private MySqliteOpenHelper helper;
    private static DbUtils dbUtils;

    private DbUtils() {

    }

    public static DbUtils instance(Context context){
        if(dbUtils == null){
            dbUtils = new DbUtils();
            dbUtils.helper = new MySqliteOpenHelper(context);
        }
        return dbUtils;
    }

    public ArrayList<String> getAllPlayer(){
        SQLiteDatabase db = helper.getReadableDatabase();
        Cursor cursor = db.query("player", new String[]{"name"}, "deltag=?", new String[]{"0"}, null, null, "pinyin");
        ArrayList<String>allPlayers = new ArrayList<String>();
        if(cursor!=null&&cursor.getCount() > 0){
            while(cursor.moveToNext()){
                String name = cursor.getString(0);
                allPlayers.add(name);
            }
        }
        cursor.close();
//        db.close();
        return allPlayers;
    }

    public int delName(String name){
        SQLiteDatabase db = helper.getWritableDatabase();
        ContentValues contentValues = new ContentValues();
        contentValues.put("deltag",1);
        int delete = db.update("player",contentValues,"name=?",new String[]{name});
//        db.close();
        return delete;
    }

    public String getLastSign(String name) {
        String uid = getUid(name);
        String lastSign = null;
        if (uid != null) {
            SQLiteDatabase db = helper.getReadableDatabase();
            Cursor cursor = db.query("sign",new String[]{"_date"},"uid=?",new String[]{uid},null,null,"_date DESC");
            if (cursor != null && cursor.getCount() > 0) {
                if (cursor.moveToNext()) {
                    lastSign = cursor.getString(0);
                }
            }
            cursor.close();
//            db.close();
        }
        return lastSign;
    }

    public String getUid(String name){
        SQLiteDatabase db = helper.getReadableDatabase();
        Cursor cursor = db.query("player", new String[]{"_id"}, "name=?", new String[]{name}, null, null, null);
        String uid = null;
        if (cursor != null && cursor.getCount() > 0) {
            if (cursor.moveToNext()) {
                uid = cursor.getString(0);
            }
        }
        cursor.close();
//        db.close();
        return uid;
    }

    public long sign(String name,String date){
        String uid = getUid(name);
        long result = 0;
        if(uid != null) {
            SQLiteDatabase db = helper.getWritableDatabase();
            Cursor cursor = db.query("sign", null, "uid=? and _date=?", new String[]{uid, date}, null, null, null);
            if (cursor != null && cursor.getCount() > 0) {
                result = -2;
            }else {
                ContentValues values = new ContentValues();
                values.put("_date", date);
                values.put("uid", uid);
                result = db.insert("sign", null, values);
            }
            cursor.close();
//            db.close();
        }
        return result;
    }


    public ArrayList<String> getSign(String name) {
        String uid = getUid(name);
        SQLiteDatabase db = helper.getReadableDatabase();
        Cursor cursor = db.query("sign", new String[]{"_date"}, "uid=?", new String[]{uid}, null, null, "_date desc");
        ArrayList<String> signList = new ArrayList<>();
        if (cursor != null && cursor.getCount() > 0) {
            while (cursor.moveToNext()){
                String date = cursor.getString(0);
                signList.add(date);
            }
        }
        cursor.close();
//        db.close();
        return signList;
    }

    public int delSign(String name, String date) {
        String uid = getUid(name);
        SQLiteDatabase db = helper.getWritableDatabase();
        int result = db.delete("sign", "uid=? and _date=?", new String[]{uid, date});
//        db.close();
        return result;
    }

    public long addPlayer(String name){
        SQLiteDatabase db = helper.getWritableDatabase();
        Cursor cursor = db.query("player", new String[]{"name"}, "name=?", new String[]{name}, null, null, null);
        long insert = 0;
        if(cursor !=null && cursor.getCount() > 0){
            //player already exist
            insert = -2;
        }else{
            String pinYin = PinYin.getPinYin(name);
            System.out.println(pinYin);
            ContentValues values = new ContentValues();
            values.put("name", name);
            values.put("pinyin", pinYin);
            insert = db.insert("player", null, values);
        }
        cursor.close();
//        db.close();
        return insert;
    }

    public  int getSignCount(String  name){
        String uid = getUid(name);
        SQLiteDatabase db = helper.getReadableDatabase();
        Cursor cursor = db.query("sign", null, "uid=?", new String[]{uid}, null, null, null);
        int result = 0;
        if (cursor != null && cursor.getCount() > 0) {
            result = cursor.getCount();
        }
        cursor.close();
//        db.close();
        return result;
    }

    public ArrayList<Player> getAllPlayerData() {
        SQLiteDatabase db = helper.getReadableDatabase();
        Cursor playerCursor = db.query("player", null, null, null, null, null, null);
        ArrayList<Player> players = new ArrayList<Player>();
        if (playerCursor != null && playerCursor.getCount() > 0) {
            while (playerCursor.moveToNext()){
                Player player = new Player();
                player._id = playerCursor.getString(0);
                player.name = playerCursor.getString(1);
                player.pinYin = playerCursor.getString(2);
                player.deltag = playerCursor.getString(3);
                player.head = playerCursor.getString(4);
                players.add(player);
            }
        }
        playerCursor.close();
//        db.close();
        return players;
    }

    public ArrayList<Sign> getAllSignData(){
        SQLiteDatabase db = helper.getReadableDatabase();
        Cursor signCursor = db.query("sign", null, null, null, null, null, null);
        ArrayList<Sign> signs = new ArrayList<Sign>();
        if (signCursor != null && signCursor.getCount() > 0){
            while (signCursor.moveToNext()){
                Sign sign = new Sign();
                sign._id = signCursor.getString(0);
                sign.date = signCursor.getString(1);
                sign.uid = signCursor.getString(2);
                signs.add(sign);
            }
        }
        signCursor.close();
//        db.close();
        return signs;
    }

    public void restorePlayer(ArrayList<Player> playerList){
        SQLiteDatabase db = helper.getWritableDatabase();
        for (Player player : playerList) {
            ContentValues values = new ContentValues();
            values.put("_id",player._id);
            values.put("name",player.name);
            values.put("pinYin",player.pinYin);
            values.put("deltag",player.deltag);
            values.put("head",player.head);
            db.insert("player",null,values);
        }
//        db.close();
    }

    public void restoreSign(ArrayList<Sign> signList){
        SQLiteDatabase db = helper.getWritableDatabase();
        for (Sign sign : signList) {
            ContentValues values = new ContentValues();
            values.put("_id",sign._id);
            values.put("_date",sign.date);
            values.put("uid",sign.uid);
            db.insert("sign",null,values);
        }
//        db.close();
    }

    public void cleanData() {
        SQLiteDatabase db = helper.getWritableDatabase();
        db.delete("player", null, null);
        db.delete("sign", null, null);
//        db.close();
    }

    //修改姓名
    public int changeName(String oldName, String newName){
        SQLiteDatabase db = helper.getWritableDatabase();
        Cursor cursor = db.query("player", null, "name=?", new String[]{newName}, null, null, null);
        int result = 0;
        if (cursor != null && cursor.getCount() > 0) {
            result = -1;
        }else {
            ContentValues values = new ContentValues();
            values.put("name",newName);
            result = db.update("player", values, "name=?", new String[]{oldName});
        }
        return result;
    }

    //补签
    public long afterSign(String name, String date){
        String uid = getUid(name);
        SQLiteDatabase db = helper.getWritableDatabase();
        Cursor cursor = db.query("sign", null, "uid=? and _date=?", new String[]{uid, date}, null, null, null);
        long result = 0;
        if(cursor != null && cursor.getCount() > 0){
            result = -1;
        }else {
            ContentValues values = new ContentValues();
            values.put("uid",uid);
            values.put("_date",date);
            result = db.insert("sign", null, values);
        }
        return result;
    }
}
